package com.wthink.hzgj.util

import android.content.Context
import android.util.TypedValue
import kotlinx.coroutines.isActive
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*


object DpUtil {

    fun px2dp(context: Context, pxValue: Float): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            pxValue,
            context.getResources().getDisplayMetrics()
        ).toInt()
    }

    fun px2sp(context: Context, pxValue: Float): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            pxValue,
            context.getResources().getDisplayMetrics()
        ).toInt()
    }

    fun dp2px(context: Context, dpValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    fun sp2px(context: Context, spValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }


    fun dayFormate(date: String): String {
        if (date.isEmpty()) {
            return ""
        }
        val string = "200702091400"
        val substring = date.substring(2)
        /*val month = substring.substring(0, 2)
        val day = substring.substring(2,4)*/
        val hour = substring.substring(4, 6)
        val minute = substring.substring(6, 8)
        //return month+"/"+day+" " + hour + ":" + minute
        return hour + ":" + minute
    }

    fun dateFormate(date: String): String {
        if (date.isEmpty()) {
            return ""
        }
        val string = "200702091400"
        val year = date.substring(0, 2)
        val month = date.substring(2, 4)
        val day = date.substring(4, 6)
        return "20" + year + "/" + month + "/" + day
    }

    fun getWeek(time: Long): String? {
        val cd = Calendar.getInstance()
        cd.time = Date(time)
        val year = cd[Calendar.YEAR] //获取年份
        val month = cd[Calendar.MONTH] //获取月份
        val day = cd[Calendar.DAY_OF_MONTH] //获取日期
        val week = cd[Calendar.DAY_OF_WEEK] //获取星期
        val weekString: String
        weekString = when (week) {
            Calendar.SUNDAY -> "周日"
            Calendar.MONDAY -> "周一"
            Calendar.TUESDAY -> "周二"
            Calendar.WEDNESDAY -> "周三"
            Calendar.THURSDAY -> "周四"
            Calendar.FRIDAY -> "周五"
            else -> "周六"
        }
        return weekString
    }

    fun getDate(): String? {
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        val mix = PreferencesUtils.getLong("mix")
        val nowTime: BigDecimal =
            BigDecimal(mix).add(BigDecimal(System.currentTimeMillis()))
        return sdf.format(nowTime)
    }

    fun getTime(): String? {
        val sdf = SimpleDateFormat("HH:mm:ss")
        val mix = PreferencesUtils.getLong("mix")
        val nowTime: BigDecimal =
            BigDecimal(mix).add(BigDecimal(System.currentTimeMillis()))
        return sdf.format(nowTime)
    }

    fun parse8B07Time(time: String): String {
        // 200922135512
        val substring = time.substring(0, 2)
        val substring1 = time.substring(2, 4)
        val substring2 = time.substring(4, 6)
        val substring3 = time.substring(6, 8)
        val substring4 = time.substring(8, 10)
        val substring5 = time.substring(10, 12)
        return "20$substring-$substring1-$substring2 $substring3:$substring4:$substring5"
    }

    fun parseList(list: List<String>): String {

        var sb =  StringBuffer()
        if (list.isNotEmpty()) {
            list.forEachIndexed { index, s ->
                if (index < list.size - 1) {
                    sb.append("$s,")
                } else {
                    sb.append("$s")
                }
            }
        }

        return sb.toString()
    }
}